package com.cy.sc.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * <br>@class: MyFilter
 * <br>@description: 自定义路由过滤器
 * <br>@author: cy
 * <br>@date: 2020-04-11 22:44:58
 * <br>@version: 1.0
 * <br>
 * <br>@since: (版本) 作者 时间 注释
 */
@Component
public class MyFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //过滤规则
        String username = exchange.getRequest().getQueryParams().getFirst("token");
        if (username == null) {
            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
            return exchange.getResponse().setComplete();
        }
        //chain 过滤链
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        //加载过滤器的优先级，0为最高
        return 0;
    }
}
